package gdou.gym.service.admin.impl;

import gdou.gym.dao.AdminMapper;
import gdou.gym.entity.Admin;
import gdou.gym.entity.AdminExample;
import gdou.gym.exception.StateCode;
import gdou.gym.exception.SysException;
import gdou.gym.service.admin.AdminService;
import gdou.gym.util.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Override
    /*验证token使用的*/
    public Admin findAdminByAccount(String userID) {
        AdminExample adminExample = new AdminExample();
        adminExample.createCriteria().andAccountEqualTo(userID);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        return admins.get(0);
    }

    @Override
    /*admin登录*/
    public Admin checkLogin(Admin admin) {
        AdminExample adminExample = new AdminExample();
        String account = admin.getAccount();
       String psw = MD5Util.getMD5(admin.getPsw());
      // String  psw=admin.getPsw();
        adminExample.createCriteria().andPswEqualTo(psw).andAccountEqualTo(account);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        if (admins.size() != 1) {
            throw new SysException(StateCode.USER_AccountOrPswERROR);
        }
        return admins.get(0);
    }

    @Override
    public void addAdmin(Admin admin) {
        if (adminMapper.insert(admin) != 1) {
            throw new SysException(StateCode.Insert_Error);
        }
    }
}
